#Chromosome.pm
# A Class for an SVG chromosome

# new
# Initializes the chromosome
package Chromosome;
use strict;

sub new 
{
	my $class = shift;
	my $self = {};
	$self->{chromosome} = shift;
	$self->{parm} = shift;
	$self->{qarm} = shift;
	$self->{npbands} = 0;
	$self->{nqbands} = 0;
	return bless $self;
}

sub get_Chrom
{
	my $self = shift;
	return $self->{chromosome};
}

sub add_Band
{
	my $self = shift;
	my $arm = shift;
	my $name = shift;
	my $colour = shift;
	my $start = shift;
	my $stop = shift;
	if($arm  eq "p")
	{
		
		
		my %phash = ('name' => $name,'colour'=>$colour, 'stop' => $stop, 'start' => $start);
		$self->{pbanding}[$self->{npbands}++] = \%phash;
	}
	elsif($arm eq "q")
	{
		my %qhash = ('name' => $name,'colour'=>$colour, 'stop' => $stop, 'start' => $start);
		$self->{qbanding}[$self->{nqbands}++] = \%qhash;
	}
}

#The chromosome has to be able to render itself in SVG format in the page
# To do this it needs to have the SVG object reference and a starting position.

sub draw_self
{
	#Each chromosome is represented along a centromeric axis with its p and q arms as a percentage of chromomosome 1
	my $self = shift;
	my $svg = shift; #$self->{SVG};
	my $cbands = shift; #$self->{cbands};
	my $positions =shift;
	my $featurebands = shift;
	my $xcent= $positions->{xcent};
	my $ycent= $positions->{ycent};
	my $width = $positions->{width};
	my $height = $positions->{height};
	my $parm = $positions->{parm};
	my $qarm = $positions->{qarm};
	my $refbase = "http://gai.nci.nih.gov/cgi-bin/histo.cgi?c=";
	my $refend = "&o=h";
	#my $svg = $self->{SVG};
	my $pbands = $self->{pbanding};
	my $qbands = $self->{qbanding};
	#print $cbands, "\n";
	my @cbkeys = keys %$cbands;
#	print "pbands exists" if($pbands);
	#Draw a chromosome based on the above
	#Calculate the parm y coordinate
	my $ph = $height *$parm;
	my $qh = $height *$qarm;
	my $y = $ycent - $ph;
	my $rx = $width /5;
	my %baseStyle = ('stroke'=>'black', 'fill'=>'rgb(60,191,247)', 'stroke-width'=>'1');
	my %boundStyle = ('stroke'=>'black', 'fill-opacity' => 0,  'stroke-width'=>'1');
	my %fill = ('acen' => 'rgb(190,190,190)', 'gneg' => 'rgb(255,255,255)', 'gpos' =>'rgb(0,0,0)', 'gvar'=>'rgb(129,129,129)', 'stalk'=>'rgb(255,255,255)', 'gpos100' => 'rgb(0,0,0)', 'gpos75' => 'rgb(66,66,66)', 'gpos50' => 'rgb(106,106,106)', 'gpos25' => 'rgb(192,192,192)');
	#Draw the basic outline
	if($self->{acro} eq 'no')
	{
	#	print "band = ", $self->{chromosome}, "p\n";
	   # if($pbands)
	   # {
		#$svg->rectangle(x=>$xcent, y=>$y, width=>$width, height=>$ph, rx=>$rx,ry=>$rx, id=>($self->{chromosome}.'p'), %baseStyle);
	   # }
		
	}
	$svg->rectangle(x=>$xcent, y=>$ycent, width=>$width, height=>$qh, rx=>$rx,ry=>$rx, id=>($self->{chromosome}.'q'), %baseStyle);
	
	#Go through each of the bands and draw as necessary.
	if($self->{acro} eq 'yes')
	{
		#have to go through the bands the opposite way
		#Take the last band and make it a satellite
	#	print "Acrocentric\n";
		my $pband = $pbands->[scalar @$pbands -1];
		my $by = $ycent - ($ph * $pband->{stop});
		my $bh = ($pband->{stop} - $pband->{start}) * $ph;
		#my $bname = $self->{chromosome} . "p". $pband->{name};
		my $bname = $self->{chromosome} . $pband->{name};
#		print "bandname = $bname\n";
		#$svg->rectangle(x=>$xcent, y=>$by, width=>$width, height=>$bh, id=>$bname, %boundStyle);
		my %cStyle =  ('stroke'=>'black', 'fill' => $fill{$pband->{colour}},  'stroke-width'=>'1');
		$svg->rectangle(x=>$xcent, y=>$by, width=>$width, height=>$bh, rx=>$rx,ry=>$rx, id=>$bname, %cStyle);
		$pband = $pbands->[scalar @$pbands -2]; #Second to last band left as space here
		#Third band done as a box
		$pband = $pbands->[scalar @$pbands -3];
		$by = $ycent - ($ph * $pband->{stop});
		$bh = ($pband->{stop} - $pband->{start}) * $ph;
		#$bname = $self->{chromosome} . "p". $pband->{name};
		$bname = $self->{chromosome} .  $pband->{name};
		#Third band done as a pTerminal
	#	$svg->rectangle(x=>$xcent, y=>$by, width=>$width, height=>$bh, rx=>$rx,ry=>$rx, id=>$bname, %baseStyle);
		%cStyle =  ('stroke'=>'black', 'fill' => $fill{$pband->{colour}},  'stroke-width'=>'1');
	       	_drawPterminal($svg, $by, $bh, $width, $xcent, \%cStyle, $bname, $self->{chromosome});
		#Draw the centromere as a q terminal
		$pband = $pbands->[0];
		$by = $ycent - ($ph * $pband->{stop});
		$bh = ($pband->{stop} - $pband->{start}) * $ph;
		$bname = $self->{chromosome} . $pband->{name};
		%cStyle =  ('stroke'=>'black', 'fill' => $fill{$pband->{colour}},  'stroke-width'=>'1');
	       	_drawQterminal($svg, $by, $bh, $width, $xcent, \%cStyle, $bname, $self->{chromosome});
		for(my $x = (scalar @$pbands -4); $x>0;$x--)
		{
			#printing
			my $pband = $pbands->[$x-1];
			my $bandstart = $pband->{start};
			my $bandstop = $pband->{stop};
			my $by = $ycent - ($ph * $bandstop);
			my $bh = ($bandstop - $bandstart) * $ph;
		#	my $bname = $self->{chromosome} . "p". $pband->{name};
			my $bname = $self->{chromosome} .  $pband->{name};
		#	print "$bname = band\n";
			if(exists $cbands->{$bname})
			{
				my %cStyle =  ('stroke'=>'black', 'fill' => $cbands->{$bname},  'stroke-width'=>'1');
				my $href= $refbase.$self->{chromosome}.$refend;
				#my $tag = $svg->anchor(-href=>$href, -target=>'new_window');
			#	tag->rectangle(x=>$xcent, y=>$by, width=>$width, height=>$bh, id=>$bname, %cStyle);
				$svg->rectangle(x=>$xcent, y=>$by, width=>$width, height=>$bh, id=>$bname, %cStyle);
			}
			else
			{
				$svg->rectangle(x=>$xcent, y=>$by, rx=>$rx, ry=>$rx, width=>$width, height=>$bh, id=>$bname, %baseStyle);
			}
		}			
	}
	else
	{
		my $counter=0;
		foreach my $pband (@$pbands)
		{
			my $bandstart = $pband->{start};
			my $bandstop = $pband->{stop};
			my $by = $ycent - ($ph * $bandstop);
			my $bh = ($bandstop - $bandstart) * $ph;
		#	my $bname = $self->{chromosome} . "p". $pband->{name};
			my $bname = $self->{chromosome} .  $pband->{name};
			$counter++;
			#print $bname, ":\n";
			#print $cbands{$bname}, "\n";
		#	print "Drawing $bname, $counter,", scalar @$pbands, "<br>\n";
			if(exists $cbands->{$bname})
			{
				#print "$bname exists\n";
				my %cStyle =  ('stroke'=>'black', 'fill' => $cbands->{$bname},  'stroke-width'=>'1');
				if($counter < scalar @$pbands)
				{
					my $href= $refbase.$self->{chromosome}.$refend;
					#my $tag = $svg->anchor(-href=>$href, -target=>'new_window');
					$svg->rectangle(x=>$xcent, y=>$by, width=>$width, height=>$bh, id=>$bname, %cStyle);
				}
				else
				{
					_drawPterminal($svg, $by, $bh, $width, $xcent, \%cStyle, $bname, $self->{chromosome});
				}
				
			}
			else
			{
				#$svg->rectangle(x=>$xcent, y=>$by, width=>$width, height=>$bh, id=>$bname, %boundStyle);
			        my %cStyle =  ('stroke'=>'black', 'fill' => $fill{$pband->{colour}},  'stroke-width'=>'1');
				if($pband->{colour} eq 'acen')
				{
				   # print "Drawing centromere<br>\n";
				    	_drawQterminal($svg, $by, $bh, $width, $xcent, \%cStyle, $bname, $self->{chromosome});
				}
				elsif($counter < scalar @$pbands)
				{
					#$svg->line(x1=>$xcent, y1=>$by, x2=>($xcent +$width), y2=>$by, id=>$bname, %boundStyle);
				    #Change from drawing lines to drawing box of the correct colour
				    
				    	$svg->rectangle(x=>$xcent, y=>$by, width=>$width, height=>$bh, id=>$bname, %cStyle);
				    
				}
				else
				{
				    	_drawPterminal($svg, $by, $bh, $width, $xcent, \%cStyle, $bname, $self->{chromosome});
				}
			}
		}		
	}
	
	my $counter=0;
	foreach my $qband (@$qbands)
	{
		my $bandstart = $qband->{start};
		my $bandstop = $qband->{stop};
		$counter++;
		#print "Counter = $counter <br>\n";
		my $by = $ycent + ($qh * $bandstart);
		my $bh = ($bandstop - $bandstart) * $qh;
		#my $bname = $self->{chromosome} . "q". $qband->{name};
		my $bname = $self->{chromosome} . $qband->{name};
#print $bname, ":\n";
		#print $cbands{$bname}, "\n";
	        my $showMe = "showMe('". $bname."_features')";
		if(exists $cbands->{$bname})
		{
			#print "$bname exists\n"
		  
			#my %cStyle =  ('stroke'=>'black', 'fill' => $cbands->{$bname},  'stroke-width'=>'1', 'onclick'=>$showMe);
		        my %cStyle =  ('stroke'=>'black', 'fill' => $cbands->{$bname},  'stroke-width'=>'1');
			#Added this line to stop Rat Chromosomes being square at the top end
			if($counter == 1)
			{
			    	_drawPterminal($svg, $by, $bh, $width, $xcent, \%cStyle, $bname, $self->{chromosome} );
			}
			elsif($counter < scalar @$qbands)
			{
				my $href= $refbase.$self->{chromosome}.$refend;
			#	my $tag = $svg->anchor(-href=>$href, -target=>'new_window');
			#	$tag->rectangle(x=>$xcent, y=>$by, width=>$width, height=>$bh, id=>$bname, %cStyle);
				$svg->rectangle(x=>$xcent, y=>$by, width=>$width, height=>$bh, id=>$bname, %cStyle);
			}
			else
			{
				_drawQterminal($svg, $by, $bh, $width, $xcent, \%cStyle, $bname, $self->{chromosome} );
			}
			#$svg->rectangle(x=>$xcent, y=>$by, width=>$width, height=>$bh, id=>$bname, %cStyle);
		}
		else
		{
			#$svg->rectangle(x=>$xcent, y=>$by, width=>$width, height=>$bh, id=>$bname, %boundStyle);
		    # my %cStyle =  ('stroke'=>'black', 'fill' => $fill{$qband->{colour}},  'stroke-width'=>'1' , 'onclick'=>$showMe);
		    
		     my %cStyle =  ('stroke'=>'black', 'fill' => $fill{$qband->{colour}},  'stroke-width'=>'1');
		     if($qband->{colour} eq 'acen')
		     {
			   # print "Drawing centromere<br>\n";
			   _drawPterminal($svg, $by, $bh, $width, $xcent, \%cStyle, $bname, $self->{chromosome});
		     }
		     elsif($counter == 1)
		     {
			  _drawPterminal($svg, $by, $bh, $width, $xcent, \%cStyle, $bname, $self->{chromosome});
		     }
		     elsif($counter < scalar @$qbands)
		     {
					#$svg->line(x1=>$xcent, y1=>$by, x2=>($xcent +$width), y2=>$by, id=>$bname, %boundStyle);
				    #Change from drawing lines to drawing box of the correct colour
				    
			$svg->rectangle(x=>$xcent, y=>$by, width=>$width, height=>$bh, id=>$bname, %cStyle);
				    
		     }
		     else
		     {
			    	_drawQterminal($svg, $by, $bh, $width, $xcent, \%cStyle, $bname, $self->{chromosome});
		     }
			#if($counter < scalar @$qbands)
			#{
				#$svg->line(x1=>$xcent, y1=>($by + $bh), x2=>($xcent +$width), y2=>($by+$bh), id=>$bname, %boundStyle);
			#}
		}
		#Draw the box here and attach it to the svg after the band if there are features present
	#	if(exists $featurebands->{$bname})
	#	{
	#	    _drawFeatureBox($svg, $xcent,  $by, $featurebands->{$bname}, $bname);
	#	}

			
	}
	my $textYPos = $height + ($height/3);
	my $textXPos = $xcent + ($width /4);
	$svg->text(id=>$self->{chromosome}, x=>$textXPos, y=>$textYPos, style=> {'font' => 'Arial' ,
            'font-size' => 20,
            'fill'      => 'black'})->cdata($self->{chromosome});
}

sub _drawPterminal
{
	#my $self= shift;
	my ($svg, $by, $bh, $width, $xcent, $cstyle, $bname, $chromNo) = @_;
	#Need to draw a curve here
	#Orientate  the other way 
	my $refbase = "http://gai.nci.nih.gov/cgi-bin/histo.cgi?c=";
	my $refend = "&o=h";
	my  $curveY = $by + $bh;
	my $cf = 5; #curve factor
	my $path;
	my $xwidth = $xcent+$width;
	$path = "M".$xcent . " ".$curveY. " ";
	#Line to edge of band -10 going upwards
	$path .= "L".$xcent . " ".($by+10). " ";
	#Take a curve starting 5 points from the end 
	$path .= "C".$xcent." ".($by+5)." ";
	# refers to  x, y+height
	$path .= $xcent." ".$by." ";
	# ends at y+height x+5
	$path .= ($xcent +5)." ".$by." ";
	# Line to x +width -10
	$path .= "L".($xwidth - ( $cf)). " ". $by. " ";
	# The next curve first ref at to x +width -5, y +height
	$path .= "C".($xwidth -(0.5* $cf)). " ". $by. " ";
	# 2nd ref point x +width y +height -5
	$path .= $xwidth. " ". ($by). " ";
	# final point x+ width y +height -10
	$path .= $xwidth. " ". ($by+(2*$cf)). " ";
	# line to x+width y
	#print "xwidth = $xwidth L is L and xcent = $xcent $width = <br>\n";
	$path .= "L". $xwidth ." ". $curveY ." ". "z";
	my $href= $refbase.$chromNo.$refend;
#	my $tag = $svg->anchor(-href=>$href, -target=>'new_window');
#	$tag->path(d=>$path, id=>$bname, %$cstyle);
	$svg->path(d=>$path, id=>$bname, %$cstyle);
	return;
}

sub _drawQterminal
{
	my ($svg, $by, $bh, $width, $xcent, $cstyle, $bname,$chromNo ) = @_;
	#Need to draw a curve here
	#Orientate  the other way 
	my $refbase = "http://gai.nci.nih.gov/cgi-bin/histo.cgi?c=";
	my $refend = "&o=h";
	my  $curveY = $by + $bh;
	my $cf = 5; #curve factor
	my $path;
	my $xwidth = $xcent+$width;
	$path = "M".$xcent . " ".$by. " ";
	#Line to edge of band -10 going upwards
	$path .= "L".$xcent . " ".($curveY- (2*$cf)). " ";
	#Take a curve starting 5 points from the end 
	$path .= "C".$xcent." ".($curveY- $cf)." ";
	# refers to  x, y+height
	$path .= $xcent." ".$curveY." ";
	# ends at y+height x+5
	$path .= ($xcent +5)." ".$curveY." ";
	# Line to x +width -10
	$path .= "L".($xwidth - $cf). " ". $curveY. " ";
	# The next curve first ref at to x +width -5, y +height
	$path .= "C".($xwidth -(0.5 * $cf)). " ". $curveY. " ";
	# 2nd ref point x +width y +height -5
	$path .= $xwidth. " ". ($curveY). " ";
	# final point x+ width y +height -10
	$path .= $xwidth. " ". ($curveY-(2*$cf)). " ";
	# line to x+width y
	#print "xwidth = $xwidth L is L and xcent = $xcent $width = <br>\n";
	$path .= "L". $xwidth ." ". $by ." ". "z";
	my $href= $refbase.$chromNo.$refend;
#	my $tag = $svg->anchor(-href=>$href, -target=>'new_window');
#	$tag->path(d=>$path, id=>$bname, %$cstyle);
	$svg->path(d=>$path, id=>$bname, %$cstyle);
	return;
}
##########################################################################################
# Title: _drawFeatureBox
# Description: encodes the box which displays the features present per band into the svg
# Date: 24.08.2005
# Author: Jon
##########################################################################################
sub _drawFeatureBox
{
   my $svg = shift;
   my $xcent = shift;
   my $by = shift;
   my $features = shift;
   my $bname = shift;
   my $id = $bname."_features";
   my $hideMe = "hideMe('$id')";

   my %gStyle =  ( 'visibility' => 'hidden', 'stroke-width'=>'1' , 'onclick'=>$hideMe);
   my %rStyle =  ('stroke'=>'black', 'fill' =>'rgb(255,255,255)', 'stroke-width'=>'1');
   my %tStyle =  ('stroke'=>'black', 'font'=>'Arial', 'font-size'=>'10', 'stroke-width'=>'1');
   my $group = $svg->group(id =>$id, %gStyle);
   my $rh = scalar @$features * 12;
   $xcent -=20;
   $by -= 20;
   $group->rect(x=>$xcent, y=>$by, width=>30, height=>$rh, id=>$id.'_box', %rStyle);
   #Add the text parts one by one 
   my $counter = 1;
   foreach my $feat (@$features)
   {
       my $featID = $id.'feat'.$counter;
       $group->text(id => $featID, x=>($xcent+2), y=>($by+ ($counter *12)), %tStyle)->cdata($feat);
       $counter++;
   }
}
##########################################################################################
1;
